/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package hangmangame;

import java.util.Scanner;

/**
 *
 * @author Joana
 */
public class HangManConsoleView extends AbstractHangManView implements IHangManView{
    
    protected boolean whileloop = true;
    
    @Override
    public String drawText() {
        String line = super.drawText();
        System.out.println(line);
        return line;
    }

    @Override
    public void drawQuestion() {
        System.out.print("Insert a character: ");
        Scanner sc = new Scanner(System.in);
        String guess = sc.nextLine();

        if (!guess.isEmpty()) {
            System.out.println("Inserted character: " + guess.charAt(0));
            if (game.addGuess(guess.charAt(0))) {
                System.out.println("Good guest!");
            } else {
                System.out.println("Bad guest, number o trys available: " + game.getTrys());
            }
        }
    }

    @Override
    public void drawEndResult(int exitCode) {
        switch (exitCode) {
            case 1:
                System.out.println("You win");
                break;
            case 2:
                System.out.println("You lost, the word was: " + game.getWord());
        }
    }

    @Override
    public boolean drawAskForNewGame() {
        System.out.print("Pretende jogar novamente? (y/N): ");
        Scanner sc = new Scanner(System.in);
        String answere = sc.nextLine();

        if (!answere.isEmpty()) {
            if (answere.equals("y")) {
                return true;
            }
        }

        return false;
    }

    @Override
    public void drawEnd() {
        System.out.println("End of game");
    }

    @Override
    public void setGame(HangManGame game) {
        this.game = game;
    }

    @Override
    public void display() {
        newGame();
        while (whileloop) {
            drawText();
            drawQuestion();
            if (game.isEnd() > 0) {
                drawEndResult(game.isEnd());
                if (drawAskForNewGame()) {
                    newGame();
                } else {
                    whileloop = false;
                    drawEnd();
                }
            }
        }
    }
    
    @Override
    public void start() {
        whileloop = true;
    }

}
